import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = '/sales/api/v1';

const getInitdesc = formatMessage({
    id: 'api.message.getInitdesc',
    defaultMessage: '初始化'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询详情'
});
const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '修改'
});
const submitDesc = formatMessage({
    id: 'api.message.submitDesc',
    defaultMessage: '提交'
});
const approveDesc = formatMessage({
    id: 'api.message.approveDesc',
    defaultMessage: '复审'
});
const firstApproveDesc = formatMessage({
    id: 'api.message.firstApproveDesc',
    defaultMessage: '初审'
});
const partclaimDesc = formatMessage({
    id: 'api.message.partclaimDesc',
    defaultMessage: '零件赔付'
});
const reasonDesc = formatMessage({
    id: 'api.message.reasonDesc',
    defaultMessage: '原因分析'
});
const upgradeDesc = formatMessage({
    id: 'api.message.upgradeDesc',
    defaultMessage: '升级讨论'
});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废'
});
const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出'
});
const getSaleShipmentDesc = formatMessage({
    id: 'api.message.getSaleShipmentDesc',
    defaultMessage: '查询发运单'
});
const getSaleShipmentDetailDesc = formatMessage({
    id: 'api.message.getSaleShipmentDetailDesc',
    defaultMessage: '查询发运单清单'
});
const getProductsDesc = formatMessage({
    id: 'api.message.getProductsDesc',
    defaultMessage: '查询车型'
});
const getPartDesc = formatMessage({
    id: 'api.message.getPartDesc',
    defaultMessage: '查询备件'
});
const getDutyUnitDesc = formatMessage({
    id: 'api.message.getDutyUnitDesc',
    defaultMessage: '查询责任单位'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/kdClaim/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitdesc));

// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/kdClaims/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/kdClaims/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 新增
export const newOrder = data => request(`${baseUrl}/kdClaims`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));
//修改
export const modifyOrder = (id, data) => request(`${baseUrl}/kdClaims/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
// 提交
export const submitData = (id, rowVersion) => request(`${baseUrl}/kdClaims/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));
// 复审
export const approveData = (id, data) => request(`${baseUrl}/kdClaims/${id}/approve`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));
// 初审
export const firstApproveData = (id, data) => request(`${baseUrl}/kdClaims/${id}/firstApprove`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, firstApproveDesc));
// 零件赔付
export const partclaimData = (id, data) => request(`${baseUrl}/kdClaims/${id}/partclaim`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, partclaimDesc));
// 原因分析
export const reasonData = (id, data) => request(`${baseUrl}/kdClaims/${id}/reason`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, reasonDesc));
// 升级讨论
export const upgradeData = (id, data) => request(`${baseUrl}/kdClaims/${id}/upgrade`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, upgradeDesc));
// 作废
export const abandonData = (id, rowVersion) => request(`${baseUrl}/kdClaims/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
// 查询发运单
export const getSaleShipments = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/saleShipments/forKDClaim?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getSaleShipmentDesc));
};
// 查询发运单清单
export const getSaleShipmentDetail = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/saleShipmentDetails/forKDClaim?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getSaleShipmentDetailDesc));
};
// 查询车型
export const getProducts = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/products/forSaleOrderForecastOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getProductsDesc));
};
// 查询备件
export const getParts = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forKDClaim?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartDesc));
};
// 查询责任单位
export const getDutyUnits = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dutyUnits?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDutyUnitDesc));
};
// 导出
export const exports = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/kdClaims/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};
